﻿/**
 * Object2D
 * 
 * @version 2012.01.27
 * @author  Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
 */

#region XNA Libraries

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace main.sdk.display.object2d
{

    public abstract class Object2D : DisplayObject
    {

        #region Static Variables

        public static readonly Viewport2D DefaultViewport = new Viewport2D();

        #endregion

        #region Instance Variables

        private Viewport2D _Viewport = DefaultViewport;

        public Viewport2D Viewport
        {
            get { return _Viewport; }
            set
            {
                _Viewport.Remove(this);
                _Viewport = value == null ? DefaultViewport : value;
                _Viewport.Add(this);
            }
        }

        public Vector2       Position        = Vector2.Zero;
        public Rectangle     SourceRectangle;
        public Color         Color           = Color.White;
        public float         Rotation        = 0.0f;
        public Vector2       Origin          = Vector2.Zero;
        public Vector2       Scale           = Vector2.One;
        public SpriteEffects Effects         = SpriteEffects.None;
        public float         LayerDepth      = 0.0f;

        protected float _Alpha = 1.0f;

        public uint Alpha
        {
            get { return (uint)(_Alpha * 255); }
            set { _Alpha = (value > 255 ? 255 : value) / 255.0f; }
        }

        #endregion

        #region Constructor & Destructor

        public Object2D(Viewport2D viewport = null)
        {
            if (viewport != null)
                Viewport = viewport;
            _Viewport.Add(this);
        }

        public virtual void Dispose() { _Viewport.Remove(this); }

        #endregion

        #region Drawing

        public abstract void Draw(SpriteBatch spriteBatch);

        #endregion

        #region Util

        public float X
        {
            get { return Position.X; }
            set { Position.X = value; }
        }

        public float Y
        {
            get { return Position.Y; }
            set { Position.Y = value; }
        }

        public void Center()
        {
            Position.X = (Program.Width - Width) / 2;
            Position.Y = (Program.Height - Height) / 2;
        }

        public float Degrees
        {
            get { return MathHelper.ToDegrees(Rotation); }
            set { Rotation = MathHelper.ToRadians(value); }
        }

        public float OX
        {
            get { return Origin.X; }
            set { Origin.X = value; }
        }

        public float OY
        {
            get { return Origin.Y; }
            set { Origin.Y = value; }
        }

        public void OCenter()
        {
            Origin.X = Width / 2;
            Origin.Y = Height / 2;
        }

        public float XO
        {
            get { return Position.X - Origin.X; }
            set { Position.X = value + Origin.X; }
        }

        public float YO
        {
            get { return Position.Y - Origin.Y; }
            set { Position.Y = value + Origin.Y; }
        }

        public int Width { get { return SourceRectangle.Width; } }

        public int Height { get { return SourceRectangle.Height; } }

        #endregion

    }

}